<?php

function array_length($arr){
    $length = 0;
    foreach($arr as $key=>$value){
        $length++;
    }
    return $length;
}

function array_implode($arr,$needle='|'){
    $length=array_length($arr);
    $str = $needle;
    $i=0;
    while($i<$length){
        $inst=$arr[$i];
        $str = $str.$inst.$needle;
        $i++;
    }
    return $str;
}

$haserror=true;
//检查用户名
//用户名必须填写
//最少三个字符
//最多五个字符
//由字母数字下划线组成
$username = $_POST["Username"];
if(!$username){
    echo "用户名必须填<br/>";
    $haserror=false;
}else{
    $len=strlen($username);
    if($len<3){
        echo "用户名至少有三个字符<br/>";
        $haserror=false;
    }else if ($len>5)
    {
        echo "用户名最多有五个字符<br/>";
        $haserror=false;
    }else if (!preg_match('/^[a-zA-Z0-9_]*$/',$username)){
        echo "格式不符合要求<br />";
        $haserror=false;
    }
}
//检查密码
//密码必须填写
//最少三个字符
//最多五个字符

$password = $_POST["password"];
if(!$password){
    echo "密码必须填<br/>";
    $haserror=false;
}else{
    $len=strlen($password);
    if($len<3){
        echo "密码至少有三个字符<br/>";
        $haserror=false;
    }else if ($len>5)
    {
        echo "密码最多有五个字符<br/>";
        $haserror=false;
    }
}

$gender = $_POST['gender'];
if($gender=='male') $gender=1;
else if ($gender='female') $gender = 0;
else $gender = -1;

//岁数
//可填写可不填写
//如果填写了，必须是正确年龄

$Age = $_POST["Age"];
if($Age){
    $AgeNum = intval($Age);
    if(!$AgeNum||$AgeNum<0||$AgeNum>200){
        echo "请填写正确的岁数<br/>";
        $haserror=false;
    }
}
//检查兴趣
//最少两个，最多三个
//

$interestes = $_POST["interestes"];

$length = array_length($interestes);

    if($length<2){
        echo "兴趣最少有两个<br/>";
        $haserror=false;
    }else if($length>3){
        echo "兴趣最多选三个<br/>";
        $haserror=false;
    }
    $interestStr = array_implode($interestes,'|');

    $province = $_POST["Province"];
    $intro = $_POST["intro"];

    if($haserror){
        $sql = "INSERT INTO 'accoun' ('username','password','gender','interestes','province','intro')
        VALUES ('$username','$password','$gender','$interestStr','$province','$intro')";    
        echo $sql;
        $sqli = new mysqli("localhost","root","","mybolg");
        $sqli -> query($sql);

    }

echo "<a href='from2.html'>返回</a><hr/>";
?>